﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using OpenExpressApp.Editors;

namespace OpenExpressApp.Module.WPF.Editors
{
    class GDataGridStringColumn : OpenDataGridColumn
    {
        private StringPropertyEditor _editor;

        public GDataGridStringColumn(BusinessObjectPropertyInfo info, IListObjectView view)
            : base(info, view)
        {
            this._editor = new StringPropertyEditor(PropertyInfo, view);
        }

        public override IWPFPropertyEditor Editor
        {
            get
            {
                return this._editor;
            }
        }

        protected override bool CommitCellEdit(FrameworkElement editingElement)
        {
            TextBox textBox = editingElement as TextBox;
            if (textBox != null)
            {
                BindingExpression binding = textBox.GetBindingExpression(TextBox.TextProperty);
                if (null != binding) binding.UpdateSource();
                return !Validation.GetHasError(textBox);
            }

            return true;
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            TextBox textBox = editingElement as TextBox;
            if (textBox != null)
            {
                textBox.Focus();
                return textBox.Text; ;
            }

            return null;
        }
    }
}
